package com.cjx.stars.controller;

import com.cjx.stars.common.MyException;
import com.cjx.stars.common.Response;
import com.cjx.stars.common.ResponseUtils;
import com.cjx.stars.pojo.request.AddCommentRequest;
import com.cjx.stars.pojo.table.BlogComments;
import com.cjx.stars.service.BlogCommentsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.cjx.stars.constant.FailMessage.ERROR_PARAMETER_IS_BLACK;
import static com.cjx.stars.constant.FailMessage.PARAMETER_ABNORMAL;

/**
 * @author cjx
 * @description 博文评论请求类
 * @createDate 2024/4/4 14:50
 **/
@RequestMapping("/comments")
@RestController
@Log4j2
public class CommentsController {

    @Resource
    BlogCommentsService blogCommentsService;

    @PostMapping("/add")
    public Response<?> addComment(@RequestBody AddCommentRequest commentRequest) {
        if (commentRequest == null) {
            return ResponseUtils.fail(ERROR_PARAMETER_IS_BLACK);
        }
        return blogCommentsService.addComment(commentRequest);
    }

    @GetMapping()
    public Response<?> getCommentByBlogId(@RequestParam Long blogId) {
        if (blogId == null || blogId <= 0) {
            return ResponseUtils.fail(ERROR_PARAMETER_IS_BLACK);
        }
        return blogCommentsService.getCommentList(blogId);
    }

    @PutMapping("/like/{commentId}")
    public Response<?> addCommentLike(@PathVariable Long commentId) {
        if (commentId == null || commentId <= 0) {
            return ResponseUtils.fail(ERROR_PARAMETER_IS_BLACK);
        }
        return blogCommentsService.addCommentLike(commentId);
    }

    @GetMapping("/{commentId}")
    public Response<?> getCommentById(@PathVariable Long commentId) {
        if (commentId == null || commentId <= 0) {
            return ResponseUtils.fail(ERROR_PARAMETER_IS_BLACK);
        }
        BlogComments commentById = blogCommentsService.getCommentById(commentId);
        return ResponseUtils.success(commentById);
    }


    @DeleteMapping("/{commentId}")
    public Response<?> deleteCommentById(@PathVariable Long commentId) {
        if (commentId == null || commentId <= 0) {
            return ResponseUtils.fail(ERROR_PARAMETER_IS_BLACK);
        }
        log.info("删除"+commentId+"号评论");
        //减少该博文的评论数
        return  blogCommentsService.deleteById(commentId);
    }

    @GetMapping("/list/my")
    public Response<?> getMyCommentList(@RequestParam int currentPage) {
        if (currentPage <= 0) {
            throw new MyException(PARAMETER_ABNORMAL);
        }
        return blogCommentsService.getMyCommentList(currentPage);
    }

}
